###########################################################
#     Prj: WGCNA all in one
#     Assignment: check scale free
#     Author: Shawn Wang
#     Date: Mar 24,2021
###########################################################

# options and package -----------------------------------------------------
load("~/02.MyScript/OneStepWGCNA/04.OneInAll/functions.Rdata")
suppressMessages(library(ggplot2))
suppressMessages(library(stringr))
suppressMessages(library(reshape2))
suppressMessages(library(ggprism))
suppressMessages(library(patchwork))
suppressMessages(library(tidyverse))
suppressMessages(library(WGCNA))
suppressMessages(library(getopt))
allowWGCNAThreads()
options(stringsAsFactors = F)
# args --------------------------------------------------------------------
command=matrix(c(
  'help', 'h', 0, 'logic', 'help information',
  'powerIn', 'p', 1, 'integer', 'test power by last step'
),byrow = T, ncol = 5)
args = getopt(command)
## default valuels==========
if (!is.null(args$help)) {
  cat(paste(getopt(command, usage = T), "\n"))
  #  q(status=1)
}
if (is.null(args$powerIn)) {
  q(status=1)
}
powerIn = args$powerIn
load("WGCNAoneInAll.Rdata")
nGenes = ncol(datExpr)
nSample = nrow(datExpr)
## test
# load("/Volumes/Samsung_T5/毕业论文/04.WGCNA/01.rawdata/test/WGCNAoneInAll.Rdata")
# powerIn = 14

# plot --------------------------------------------------------------------
p = powertest(power.test = powerIn,datExpr = datExpr,nGenes = nGenes)
ggsave(p,filename = "02.ScaleFreePowerTest.pdf",width = 8,height = 6)



